__author__ = 'Muscle'
from mui.muicomponent import MUIStatic

from mui import color
import mui.InternalUse.muiResource as muiResource
import mui.CExt.muiDX as muiDX
import mui.InternalUse.muiconfig as config
import random

class ImageBase:
    _stretchTypeMap = {'Fill': muiDX.Fill, 'Uniform':muiDX.Uniform, 'UniformFill': muiDX.UniformFill, 'NoStretch':muiDX.NoStretch}
    def __init__(self):
        self.image = ''
        self.imageOpacity = 1.0
        self.stretch = 'Uniform'
        self.autoDirty(['image', 'imageOpacity', 'stretch'], [])
        
    def getActiveImage(self):
        return self.image

    def draw(self, renderer, pos = None):
        imgPath = self.getActiveImage()
        if not imgPath:
            return
        if not pos:
            pos = self.rect
        try:
            stretchType = self._stretchTypeMap[self.stretch]
        except KeyError, e:
            stretchType =  _stretchTypeMap['Uniform']
        renderRect = (0, 0, pos[2], pos[3])
        renderer.DrawBitmap(imgPath, renderRect, stretchType, self.imageOpacity)

        
class MUIImage(MUIStatic, ImageBase):
    def __init__(self, parent=None, **kwargs):
        MUIStatic.__init__(self, parent, **kwargs)
        ImageBase.__init__(self)
        self.setDefault(**kwargs)

    def on_draw(self, renderer):
        super(MUIImage, self).on_draw(renderer)
        ImageBase.draw(self, renderer)
        if (config.g_bShowDirty):
            renderer.DrawColorRectangle((3, 3, self.width-3, self.height-3), (1,random.random(), random.random(),1), 2)
